package com.ebao.tec.service.impl;

import com.ebao.tec.entity.*;
import com.ebao.tec.mapper.CompanyMapper;
import com.ebao.tec.mapper.XjdMapper;
import com.ebao.tec.service.IndexService;
import com.ebao.tec.vo.ParentCompanyVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @DESCRIPTION:
 * @Author: WangLt & TianRen Li
 * @Date: 2020/11/27
 */
@Service
public class IndexServiceImpl implements IndexService {

    @Resource
    private CompanyMapper companyMapper;

    @Resource
    private XjdMapper xjdMapper;

    private final static String[] SYS_TYPE = {"OMS","PMS","基建","营销","财务","I6000"};

    @Override
    public List<ParentCompanyVO> listCompany() {
        ArrayList<ParentCompanyVO> res = new ArrayList<>();
        CompanyExample companyExample = new CompanyExample();
        companyExample.createCriteria().andParentsIdEqualTo(0);
        List<Company> parentCompanies = companyMapper.selectByExample(companyExample);
        for (Company parentCompany : parentCompanies) {
            companyExample = new CompanyExample();
            companyExample.createCriteria().andParentsIdEqualTo(parentCompany.getCompanyId());
            List<Company> sonCompanies = companyMapper.selectByExample(companyExample);
            res.add(new ParentCompanyVO(parentCompany.getCompanyId(),parentCompany.getCompanyName(),sonCompanies));
        }
        return res;
    }

    @Override
    public List<Map<String, Object>> RankByCompany(Integer companyId) {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = null;
        CompanyExample companyExample = new CompanyExample();
        companyExample.createCriteria().andParentsIdEqualTo(companyId);
        List<Company> companies = companyMapper.selectByExample(companyExample);
        XjdExample xjdExample = null;
        for (Company company : companies) {
            map = new HashMap<>();
            xjdExample = new XjdExample();
            xjdExample.createCriteria().andJxCompanyIdEqualTo(company.getCompanyId());
            Long count = xjdMapper.countByExample(xjdExample);
            map.put("company", company.getCompanyName());
            map.put("count", count);
            list.add(map);
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> RankByJxSys(Integer companyId) {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = null;
        CompanyExample companyExample = new CompanyExample();
        companyExample.createCriteria().andParentsIdEqualTo(companyId);
        List<Company> companies = companyMapper.selectByExample(companyExample);
        XjdExample xjdExample = null;
        List<Integer> companyIdList = new ArrayList<>();
        for (Company company : companies) {
           companyIdList.add(company.getCompanyId());
        }
        for (String s : SYS_TYPE) {
            map = new HashMap<>();
            xjdExample = new XjdExample();
            xjdExample.createCriteria().andJxCompanyIdIn(companyIdList).andJxSysEqualTo(s);
            long count = xjdMapper.countByExample(xjdExample);
            map.put("count", count);
            map.put("sys", s);
            list.add(map);
        }

        return list;
    }

    @Override
    public List<Xjd> listXjdByCompanyId(Integer companyId) {
        XjdExample xjdExample = new XjdExample();
        xjdExample.createCriteria().andJxCompanyIdEqualTo(companyId);
        List<Xjd> xjds = xjdMapper.selectByExample(xjdExample);
        return xjds;
    }

}
